package com.mrkun.vueshop.controller;

import com.mrkun.vueshop.DTO.MoneyDTO;
import com.mrkun.vueshop.mapper.OrderMapper;
import com.mrkun.vueshop.model.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author by yong
 * @date 2020/3/22
 */
@RestController
public class OrderController {

    @Autowired
    private OrderMapper orderMapper;

    @GetMapping("/order/get_by_user_id/{userId}")
    public List<Order> getAll(@PathVariable("userId")Integer userId){
        return orderMapper.findByUserId(userId);
    }

    @GetMapping("/order/get_money/{userId}")
    public Map<String, BigDecimal> getMoney(@PathVariable("userId")Integer userId){
        Map<String,BigDecimal> map = new ConcurrentHashMap<>(2);
        MoneyDTO totalMoney = orderMapper.getTotalMoney(userId);
        map.put("totalMoney",totalMoney.getTotalMoney());
        map.put("totalIncome",totalMoney.getTotalIncome());
        return map;
    }

    @PostMapping("/order/sell_fund/{orderId}")
    public String sell(@PathVariable("orderId")Integer orderId){
        try{
            orderMapper.sellFund(orderId);
            return "success";
        }catch (Exception e){
            e.printStackTrace();
            return "failure";
        }
    }
}
